import FileSaver from 'file-saver'
import XLSX from 'xlsx'
export default {
  data: {
    echarts_data: {
      sameOp: {
        color: ['red', 'yellow', 'green'],
        grid: {
          left: '3%',
          right: '8%',
          bottom: '6%',
          containLabel: true
        },
        tooltip: {
          trigger: 'axis'
        },
        toolbox: {
          iconStyle: {
            borderColor: '#fff'
          },
          feature: {
            dataView: {
              show: true,
              lang: ['数据视图', '关闭', '导出Excel'],
              //optionToContent为重画表格的函数
              optionToContent: function(opt) {
                //axisData是你想定义的表格第一列的数据，我这里设置为柱形图的x轴数据
                var axisData = opt.xAxis[0].data
                //tAxis[0]为你想定义的表格第一行的数据
                var txisData = opt.legend[0].data
                console.log(opt)
                var series = opt.series
                //表头
                var tdHeads = '<td  style="padding: 0 10px"></td>'
                var tdBodys = ''
                var nameData = txisData
                console.log(nameData)
                for (var i = 0; i < nameData.length; i++) {
                  tdHeads +=
                    '<td style="padding: 0 10px">' + nameData[i] + '</ td >'
                }
                var table =
                  '<table id="Mytable" border="1" class="table table-bordered table-striped table-hover" style="width:100%;text-align:center;color: #000;" ><tbody><tr>' +
                  tdHeads +
                  ' </tr>'
                for (var i = 0, l = axisData.length; i < l; i++) {
                  for (var j = 0; j < series.length; j++) {
                    if (series[j].data != undefined) {
                      var temp = series[j].data[i]
                      if (temp != null && temp != undefined) {
                        tdBodys += '<td>' + temp + '</td>'
                      } else {
                        tdBodys += '<td></td>'
                      }
                    }
                  }
                  table +=
                    '<tr><td style="padding: 0 10px">' +
                    axisData[i] +
                    '</td>' +
                    tdBodys +
                    '</tr>'
                  tdBodys = ''
                }
                table += '</tbody></table>'
                return table
              },
              //contentToOption为重写“刷新”按钮的语句
              contentToOption: function(HTMLDomElement, opt) {
                let et = XLSX.utils.table_to_book(
                  document.getElementById('Mytable')
                )
                let etout = XLSX.write(et, {
                  bookType: 'xlsx',
                  bookSST: true,
                  type: 'array'
                })
                try {
                  FileSaver.saveAs(
                    new Blob([etout], {
                      type: 'application/octet-stream'
                    }),
                    'echarts数据' + '-' + new Date().toLocaleString() + '.xlsx'
                  )
                } catch (e) {}
                return etout
              }
            },
            magicType: { show: true },
            restore: { show: true },
            saveAsImage: { show: true },
            myTool: {
              show: true, //是否显示
              title: '定义y轴', //鼠标悬空的提示文字
              icon:
                'path://M659.77002 969.890638 363.679441 969.890638 363.679441 644.667555 33.604864 76.752058l325.225129 0 155.319461 300.952306L669.480172 76.752058 989.844597 76.752058 659.77002 639.812991 659.77002 969.890638z', //这个是阿里icon svg 后 b的全部字符串
              onclick: o => {
                window.Definethey(o)
              }
            }
          }
        },
        legend: {
          data: [],
          textStyle: {
            fontSize: 20,
            color: '#fff'
          }
        },
        dataZoom: [
          {
            //X轴滑动条                    {
            type: 'slider', //滑动条
            show: true, //开启
            xAxisIndex: [0],
            bottom: '0%',
            height: 15,
            start: 0, //初始化时，滑动条宽度开始标度
            end: 100 //初始化时，滑动条宽度结束标度
          }, //X轴内置滑动
          {
            type: 'inside', //内置滑动，随鼠标滚轮展示
            xAxisIndex: [0],
            start: 0, //初始化时，滑动条宽度开始标度
            end: 100 //初始化时，滑动条宽度结束标度
          }
        ]
      }
    }
  }
}
